// main.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:'',
    cardStatus:'我丢卡了',
    useLostCardLog:'zan-btn zan-btn--warn',
    lostCardLogDisable: false,
    usePersonal:'zan-btn zan-btn--danger',
    personalDisable: false,
    count:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {        
    if (app.globalData.openid == '' || app.globalData.openid == undefined){
      wx.login({
        success: function (res) {
          wx.request({
            url: app.globalData.baseUrl + '/login',
            method: 'GET',
            data: { code: res.code },
            success: function (res) {                            
              app.globalData.openid = res.data['openid'];                               app.globalData.isRegisted = res.data['isRegisted'];             
            },
            fail: function () {
              console.log("Failed");
            },            
          })
        },
        fail: function (res) {
          console.log(res.errMsg);
        },
        complete: function () {
        }
      })  
    } 
    if(app.globalData.isRegisted == false){      
      this.setData({ lostCardLogDisable: true });
      this.setData({ useLostCardLog: 'zan-btn zan-btn--disabled' });
      this.setData({ personalDisable: true });
      this.setData({ usePersonal: 'zan-btn zan-btn--disabled' });      
      if(this.data.count==2){  //每隔一次 onshow 提示注册一次
        this.data.count=0;
        wx.showModal({
          title: '进行注册？',
          content: '注册后可启用更多功能',
          showCancel: true,
          cancelText: '不注册',
          // cancelColor: '',
          confirmText: '注册',
          // confirmColor: '',
          success: function(res) {
            if(res.confirm){
              wx.redirectTo({
                url: '/pages/register/register',
              })
            }
          },
          fail: function(res) {},
          complete: function(res) {},
        })
      }
      this.data.count++;
      return
    } 
    var that = this;
    var sendData = {
      openid: app.globalData.openid
    };
    wx.request({
      url: app.globalData.baseUrl + '/isLostCardLog',
      data: sendData,
      success: function (res) {
        var rs = res.data['isExist'];
        var status = that.data.cardStatus;
        let btn = that.data.useLostCardLog;
        let lostCardLogDisable = that.data.lostCardLogDisable;
        if (rs == 1) {
          that.setData({ cardStatus: '已进行失卡登记' });
          that.setData({ useLostCardLog: 'zan-btn zan-btn--disabled' });
          that.setData({ lostCardLogDisable: 'true' });
          app.globalData.lostReported = true;
        }
        else {
          that.setData({ cardStatus: '我丢卡了' });
          that.setData({ useLostCardLog: 'zan-btn zan-btn--warn' });
          that.setData({ lostCardLogDisable: '' });
        }
      }
    }) 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  toPickCardLog:function(){
    /*检测是否注册 */
    wx.navigateTo({
      url: '../pickCardLog/pickCardLog',
    })
  },

  toLostCardLog: function(){
    wx.showModal({
      title: '确定失卡登记？',
      showCancel:true,
      content:'失卡登记后，如有匹配的拾卡信息将在微信与您的校内邮箱中提醒您',
      success:function(res){
        if(res.confirm){          
          wx.navigateTo({
            url: '../lostCardLog/lostCardLog',
          })
        }
      }
    })
  },

  toPersonality: function(){
    wx.navigateTo({
      url: '../personal/personal',
    })
  }
})